package com.zeei.ems.base.security.config;

import com.zeei.ems.base.security.bo.SessionUserInfo;
import com.zeei.ems.base.security.service.AuthService;
import com.zeei.ems.base.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;

public class UserDetailsServiceImpl  implements UserDetailsService {

    @Autowired
    AuthService authService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserVO basUser = authService.getUserByUsername(username);
        if(basUser!=null){
            User user = new SessionUserInfo(basUser.getLoginname(), basUser.getLoginpw(), new ArrayList<>(), basUser);
            basUser.setLoginpw("protect");
            return user;
        }

        throw new UsernameNotFoundException("用户："+username+"不存在!");
    }
}
